/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef, useEffect } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import DragTable from '@/components/DragTable';
import ProTable from '@ant-design/pro-table';
import type { ActionType } from '@ant-design/pro-table';
import styles from './index.less';
import { getAmountverifyList, verifyAmount } from '@/services/finance_amountverify';
import { Button, Space, Popconfirm } from 'antd';
import type { StringObjType } from '@/components/_pages/AmountApply/index.d';
import { applyCommonColumn } from '@/components/_pages/AmountApply/index.d';
import { usePageTabsChange } from '@/utils/hooks/pageTabs';

const AmountCashVerify: React.FC = () => {
  const [show] = usePageTabsChange();
  const actionRef = useRef<ActionType>();

  useEffect(() => {
    if (show) {
      actionRef.current?.reload();
    }
  }, [show]);

  const confirmAllow = async (params: { type: string; data: StringObjType }) => {
    const {
      type,
      data: { id },
    } = params;
    const { code } = await verifyAmount({ id, operate: type == 'allow' ? '1' : '2' });
    if (code == 0) {
      actionRef.current?.reload();
    }
  };

  return (
    <PageContainer className={styles.main} title=''>
      <DragTable>
        <ProTable<StringObjType>
          className='kb-pro-table'
          actionRef={actionRef}
          pagination={false}
          rowKey='id'
          request={getAmountverifyList}
          columns={[
            ...applyCommonColumn,
            {
              title: '操作',
              dataIndex: 'operate',
              align: 'center',
              search: false,
              render: (_, record) => {
                return (
                  <Space>
                    {['allow', 'reject'].map((item) => (
                      <Popconfirm
                        title={`确认${item == 'allow' ? '通过' : '拒绝'}提现申请`}
                        key={item}
                        onConfirm={confirmAllow.bind(null, {
                          type: item,
                          data: record,
                        })}
                      >
                        <Button size='small' type={item == 'allow' ? 'primary' : 'ghost'}>
                          {item == 'allow' ? '通过' : '拒绝'}
                        </Button>
                      </Popconfirm>
                    ))}
                  </Space>
                );
              },
            },
          ]}
          options={false}
          scroll={{ x: 1200 }}
          search={false}
        />
      </DragTable>
    </PageContainer>
  );
};

export default AmountCashVerify;
